package yxy.game.pm2.active.command.developgiftpackage;

import yxy.apple.logger.Logger;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.command.supfirst.CMD_Active_超值首充_Info;
import yxy.game.pm2.active.module.ActiveModule_养成礼包;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.subordinateclass.DevelopGiftPackage;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActDevelopGiftPackageInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

@MessageType(proto = Command.ActDevelopGiftPackageInfo, classtype = ActiveMessage.ActDevelopGiftPackageInfoRequest.class)
public class CMD_Active_养成礼包_Info extends Command<ActDevelopGiftPackageInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_超值首充_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActDevelopGiftPackageInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActDevelopGiftPackageInfoResponse.Builder response = ActiveMessage.ActDevelopGiftPackageInfoResponse.newBuilder();
		
		ActiveModule_养成礼包 am = ActiveModule_养成礼包.get(playercode);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		for(DevelopGiftPackage dgift : am.getDgifts()) {
			ActiveMessage.DevelopGiftPackage.Builder build = ActiveMessage.DevelopGiftPackage.newBuilder();
			
			build.setGoodsid(dgift.getGoodsid());
			build.setEndtime(dgift.getEnd().getMillis());
			build.setStar(dgift.getStar());

			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(dgift.getCurs());
			build.addAllCurs(items);
			
			int canget = dgift.isHadbuy() ? DrawStatus.hadget.getStatus() : dgift.isLock() ? DrawStatus.cannotget.getStatus() : DrawStatus.canget.getStatus();
			build.setCanget(canget);
			
			response.addDgifts(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActDevelopGiftPackageInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
